// user-module/src/main/java/org/x/user/controller/SysConfigController.java
package org.x.user.controller;

import com.mybatisflex.core.paginate.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.x.common.config.Result;
import org.x.user.dto.SysConfigDTO;
import org.x.user.entity.SysConfigEntity;
import org.x.user.entity.SysConfigHistoryEntity;
import org.x.user.service.ISysConfigService;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/config")
public class SysConfigController {

    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 获取所有系统配置
     */
    @GetMapping("/all")
    public Result<List<SysConfigEntity>> getAllConfigs() {
        List<SysConfigEntity> configs = sysConfigService.list();
        return Result.ok(configs);
    }

    /**
     * 修改系统配置
     */
    @PutMapping("/update")
    public Result<Boolean> updateConfig(@RequestBody SysConfigDTO configDTO) {
        SysConfigEntity entity = new SysConfigEntity();
        BeanUtils.copyProperties(configDTO, entity);
        entity.setUpdatedTime(new Date());

        boolean result = sysConfigService.updateById(entity);
        return Result.ok(result);
    }

    /**
     * 刷新配置缓存
     */
    @PostMapping("/refreshCache")
    public Result<Map<String, Object>> refreshCache() {
        Map<String, Object> result = sysConfigService.refreshCache();
        return Result.ok(result);
    }

    /**
     * 获取公共配置
     */
    @GetMapping("/public")
    public Result<Map<String, Object>> getPublicConfigs() {
        Map<String, Object> publicConfigs = sysConfigService.getPublicConfigsForFrontend();
        return Result.ok(publicConfigs);
    }

    /**
     * 查询配置变更历史记录
     */
    @GetMapping("/history")
    public Result<List<SysConfigHistoryEntity>> getConfigHistory(
            @RequestParam(required = false) String configKey,
            @RequestParam(required = false) Long operatorId,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {

        List<SysConfigHistoryEntity> historyList = sysConfigService.getConfigHistory(configKey, operatorId, startTime, endTime);
        return Result.ok(historyList);
    }

    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody SysConfigEntity sysConfig) {
        boolean result = sysConfigService.createConfigWithValidation(sysConfig);
        return Result.ok(result);
    }

    @PutMapping("/update-value")
    public Result<Boolean> update(@RequestBody SysConfigEntity sysConfig) {
        boolean result = sysConfigService.updateConfigValue(sysConfig.getId(), sysConfig.getConfigValue());
        return Result.ok(result);
    }

    /**
     * 分页查询系统配置表
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("/page")
    public Result<Page<SysConfigEntity>> page(Page<SysConfigEntity> page) {
        Page<SysConfigEntity> result = sysConfigService.page(page);
        return Result.ok(result);
    }
}
